﻿using System;
using System.Text;

namespace Netkk.Common
{
    public static class Tools
    {
        public static string GetCleanUrl(string dirtyUrl)
        {
            if (dirtyUrl == null)
                return String.Empty;

            const string allowedChars = "abcdefghijklmnopqrstuvwxyz1234567890-_";
            dirtyUrl = dirtyUrl.ToLower();
            dirtyUrl = dirtyUrl.Replace("\x00e4", "ae");
            dirtyUrl = dirtyUrl.Replace("\x00fc", "ue");
            dirtyUrl = dirtyUrl.Replace("\x00f6", "oe");
            dirtyUrl = dirtyUrl.Replace(" ", "-");

            StringBuilder cleanUrl = new StringBuilder();
            int i = 0;
            int length = dirtyUrl.Length;
            while (i < length)
            {
                char c = dirtyUrl[i];
                if (allowedChars.IndexOf(c) > -1)
                {
                    cleanUrl.Append(c);
                }
                i++;
            }
            return cleanUrl.ToString();
        }
    }
}